var $$ = function(id){return document.getElementById(id);};
var yourUsernameInput = $$('yourUsername');
var otherUsernameInput = $$('otherUsername');
var yourVideo = $$('your');
var otherVideo = $$('other');
var loginButton = $$('login');
var callButton = $$('call');
var leaveButton = $$('leave');
var viewer = $$('viewer');
/*首页->App ID*/
var appId = "wd0918586519vbkoqe";
var localStream = null;
var roomInstance = null;

function showError(error){
    console.log(error);
}

function roomInstanceConnected(){
    /**/
    if(! viewer.checked){
        roomInstance.publish(localStream, function(error){
            if(error == null){
                if(! $$("showSelf").checked){
                    yourVideo.srcObject = localStream.stream;
                }
            }
            else{
                console.log(error);
            }
        });
    }
    /**/
    roomInstance.on("stream_added", function(roomStream){
        roomInstance.subscribe(roomStream, function(error){
            if(error == null){
                console.log("subscribe success");
            }
        });
    });
    /**/
    roomInstance.on("stream_received", function(roomStream){
        otherVideo.srcObject = roomStream.stream;
    });
    /**/
    roomInstance.on("stream_removed", function(roomStream){});
    /**/
    roomInstance.on("disconnected", function(){});
    /**/
}

loginButton.addEventListener("click", function(event){
    wilddog.initializeApp({authDomain: appId + '.wilddog.com'});
    /*
    token参数通过如下方式生成
    http://192.168.1.20:10106/?uid=4c2c8300ab32
    http://192.168.1.20:10106/?uid=4c2c8300ab32-user
    */
    var token = {
        "device": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhZG1pbiI6ZmFsc2UsImRlYnVnIjpmYWxzZSwidWlkIjoiNGMyYzgzMDBhYjMyIiwidiI6MSwiaWF0IjoxNTE1NjYzMTYxfQ.gCrtqVpM9q7QZYXLsfi7N-FVwyQbHcxRh_JCu00dbMw",
        "user": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhZG1pbiI6ZmFsc2UsImRlYnVnIjpmYWxzZSwidWlkIjoiNGMyYzgzMDBhYjMyLXVzZXIiLCJ2IjoxLCJpYXQiOjE1MTU2NjMyMTR9.IHc35qWkcKtKKUIg5s_7odutSA0m4uFJvE00A5M7Wak"
    };
    if(window.location.href.split("#")[1] == 'auto'){
        var jwt = token.device;
    }
    else{
        var jwt = token.user;
    }
    wilddog.auth().signInWithCustomToken(jwt)
        .then(function(user){
            console.log(user);
            wilddogVideo.initialize({'appId': appId, 'token': user.getToken()});
            if(! viewer.checked){
                wilddogVideo.createLocalStream({captureAudio: false, captureVideo: true, dimension: "480p", maxFPS: 15})
                    .then(function(theStream){
                        localStream = theStream;
                    })
                    .catch(showError);
            }
        })
        .catch(showError);
});

callButton.addEventListener("click", function(){
    roomInstance = wilddogVideo.room("4c2c8300ab32");
    roomInstance.connect();
    roomInstance.on("connected", roomInstanceConnected);
});

leaveButton.addEventListener("click", function(){
    roomInstance.disconnect();
});
